﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

public partial class admin_Products_Details : System.Web.UI.Page
{
  ProdCateBLL categoryData = new ProdCateBLL();
  ProductBLL productData = new ProductBLL();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      LoadTitle();
      LoadCategory();
      if (Request.QueryString["Id"] != null)
        Filldata();
      else
      {
        txtView.Text = "0";
        trCreatedDate.Visible = false;       
      }
    }
  }
  private void Filldata()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Product product = productData.GetById(id);
    txtName.Text = product.Name;
    txtUnit.Text = product.Unit;
   // txtDescription.Text = product.Description;

    if (product.Cost != null)
      txtCost.Text = String.Format("{0:0}", ((decimal)product.Cost));

    txtTag.Text = product.Tag;
    txtView.Text = product.View.ToString();
    txtCreatedDate.Text = product.CreatedDate.ToShortDateString();
    fckEditor.Value = HttpUtility.HtmlDecode(product.Content);

    dropParent.SelectedValue = product.CategoryId.ToString();
    if (product.isEnable == true) radioEnable.Checked = true;
    else radioVisible.Checked = true;

    if (product.isKM)
        checkKM.Checked = true;
    if (product.isHot)
        checkHot.Checked = true;
     
    //Load Image
    if (product.Image_01 != null)
    {
        image_01.ImageUrl = "~/Images/Product/" + product.Image_01;
        image_01.Visible = true;
        check_Del_img1.Visible = true;
    }
    if (product.Image_02 != null)
    {
        image_02.ImageUrl = "~/Images/Product/" + product.Image_02;
        image_02.Visible = true;
        check_Del_img2.Visible = true;
    }
    if (product.Image_03 != null)
    {
        image_03.ImageUrl = "~/Images/Product/" + product.Image_03;
        image_03.Visible = true;
        check_Del_img3.Visible = true;
    }
    if (product.Image_04 != null)
    {
        image_04.ImageUrl = "~/Images/Product/" + product.Image_04;
        image_04.Visible = true;
        check_Del_img4.Visible = true;
    }
    if (product.Image_05 != null)
    {
        image_05.ImageUrl = "~/Images/Product/" + product.Image_05;
        image_05.Visible = true;
        check_Del_img5.Visible = true;
    }
  }
  private void ShowChild(int parentId, string space)
  {
    List<ProdCate> listDataSource = categoryData.GetByParent(parentId);

    foreach (ProdCate item in listDataSource)
    {
      ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
      dropParent.Items.Add(obj);

      ShowChild(item.Id, space + "- - - ");
    }
  }
  private void LoadCategory()
  {
    dropParent.Items.Clear();
    ShowChild(0, " - - ");
  }
  private void LoadTitle()
  {
    if (Request.QueryString["Id"] != null)
    {
      h3Title.InnerText = "Chỉnh sửa thông tin sản phẩm";
    }
  }
  private bool isValidate()
  {
    if (txtName.Text == "")
    {
      Myfunction.ThongBao(lbThongBao, "Tên sản phẩm không được để trống !");
      txtName.Focus();
     return false;
    }
    if (dropParent.SelectedIndex == -1)
    {
      Myfunction.ThongBao(lbThongBao, "Chọn danh mục !");
      return false;
    }
    decimal cost;
    if (txtCost.Text != "" && !decimal.TryParse(txtCost.Text.Trim(), out cost))
    {
      Myfunction.ThongBao(lbThongBao, "Nhập giá sản phẩm không hợp lệ !");
      txtCost.Focus();
      return false;
    }
    int view;
    if (txtView.Text != "" && !int.TryParse(txtView.Text.Trim(), out view))
    {
      Myfunction.ThongBao(lbThongBao, "Nhập số lượng xem không hợp lệ !");
      txtView.Focus();
      return false;
    }
    DateTime date;
    if (txtCreatedDate.Text != "" && !DateTime.TryParse(txtCreatedDate.Text.Trim(), out date))
    {
      Myfunction.ThongBao(lbThongBao, "Ngày tháng không hợp lệ !");
      txtCreatedDate.Focus();
      return false;     
    }
    if (Request.QueryString["Id"] == null)
    {
        //if (!fileUpload_thumb.HasFile)
        //{
        //    Myfunction.ThongBao(lbThongBao, "Ảnh thumbnails không được để trống !");
        //    return false;
        //}
        if (!fileUpload.HasFile)
        {
            Myfunction.ThongBao(lbThongBao, "Ảnh đại diện không được để trống !");
            return false;
        }
    }
    return true;
  }
  private void ResetForm()
  {
    txtName.Text = "";
    txtUnit.Text = "";
    fckEditor.Value = "";
   // txtDescription.Text = "";
    txtCost.Text = "";
    txtTag.Text = "";
    txtView.Text = "0";
    radioEnable.Checked = true;
  }
  private void Update()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Product product = productData.GetById(id);

    product.CategoryId = Convert.ToInt32(dropParent.SelectedValue);
    product.Name = txtName.Text;
    product.Unit = txtUnit.Text;
  //  product.Description = txtDescription.Text;
    if (txtCost.Text != "")
      product.Cost = Convert.ToDecimal(txtCost.Text.Trim());
    if (txtView.Text != "")
      product.View = Convert.ToInt32(txtView.Text.Trim());
    product.Tag = txtTag.Text;
    product.Content = HttpUtility.HtmlEncode(fckEditor.Value);
    product.CreatedDate = Convert.ToDateTime(txtCreatedDate.Text);
    product.isEnable = (radioEnable.Checked ? true : false);

    product.isKM = (checkKM.Checked ? true : false);
    product.isHot = (checkHot.Checked ? true : false);

    if (fileUpload.HasFile)
    {
        if (product.Image == null || product.Image == "")
            product.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Product", product.Name);
        else
            product.Image = Myfunction.UpdateFile(fileUpload.PostedFile, "Images/Product", product.Image, product.Name);
    }
    //if (fileUpload_thumb.HasFile)
    //{
    //    string nameCate = product.Name;
    //    try { nameCate = categoryData.GetById(product.CategoryId).Name; }
    //    catch { }
    //    if (product.Image_thumb == null || product.Image_thumb == "")
    //        product.Image_thumb = Myfunction.CopyFileThumbnail(fileUpload_thumb.PostedFile, "Images/Product", nameCate + "-" + product.Name);
    //    else
    //        product.Image_thumb = Myfunction.UpdateFileThumbnail(fileUpload_thumb.PostedFile, "Images/Product", product.Image_thumb, nameCate + "-" + product.Name);
    //}
    //Copy image phu
    if (fileUploadImage_01.HasFile && !check_Del_img1.Checked)
    {
        product.Image_01 = Myfunction.CopyFile(fileUploadImage_01.PostedFile, "Images/Product",  product.Name + "-01");
    }
    if (fileUploadImage_02.HasFile && !check_Del_img2.Checked)
    {
        product.Image_02 = Myfunction.CopyFile(fileUploadImage_02.PostedFile, "Images/Product",  product.Name + "-02");
    }
    if (fileUploadImage_03.HasFile && !check_Del_img3.Checked)
    {
        product.Image_03 = Myfunction.CopyFile(fileUploadImage_03.PostedFile, "Images/Product",  product.Name + "-03");
    }
    if (fileUploadImage_04.HasFile && !check_Del_img4.Checked)
    {
        product.Image_04 = Myfunction.CopyFile(fileUploadImage_04.PostedFile, "Images/Product", product.Name + "-04");
    }
    if (fileUploadImage_05.HasFile && !check_Del_img5.Checked)
    {
        product.Image_05 = Myfunction.CopyFile(fileUploadImage_05.PostedFile, "Images/Product", product.Name + "-05");
    }  
      //Delete image
    if (check_Del_img1.Checked)
    {
        Myfunction.DeleteFile("Images/Product", product.Image_01);
        product.Image_01 = null;        
    }
    if (check_Del_img2.Checked)
    {
        Myfunction.DeleteFile("Images/Product", product.Image_02);
        product.Image_02 = null;       
    }
    if (check_Del_img3.Checked)
    {
        Myfunction.DeleteFile("Images/Product", product.Image_03);
        product.Image_03 = null;        
    }
    if (check_Del_img4.Checked)
    {
        Myfunction.DeleteFile("Images/Product", product.Image_04);
        product.Image_04 = null;       
    }
    if (check_Del_img5.Checked)
    {
        Myfunction.DeleteFile("Images/Product", product.Image_05);
        product.Image_05 = null;        
    }
      
    productData.Update(product);

    Myfunction.ThongBao(lbThongBao, "Thông tin sản phẩm đã được cập nhật");
    Filldata();
  }
  private void Add()
  {
    Product product = new Product();

    product.CategoryId = Convert.ToInt32(dropParent.SelectedValue);
    product.Name = txtName.Text;
    product.Unit = txtUnit.Text;
  //  product.Description = txtDescription.Text;   
    if (txtCost.Text != "")
      product.Cost = Convert.ToDecimal(txtCost.Text.Trim());
    if (txtView.Text != "")
      product.View = Convert.ToInt32(txtView.Text.Trim());
    else product.View = 0;
    product.Tag = Myfunction.LocDau(txtName.Text.Trim().ToLower()) + " | "                
                 + Myfunction.LocDau(txtTag.Text.Trim().ToLower());
    product.Content = HttpUtility.HtmlEncode(fckEditor.Value);
    product.CreatedDate = DateTime.Now;

    product.isEnable = (radioEnable.Checked ? true : false);
    product.isKM = (checkKM.Checked ? true : false);
    product.isHot = (checkHot.Checked ? true : false);

    if (fileUpload.HasFile)
    {
        product.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Product", product.Name);
    }
    //if (fileUpload_thumb.HasFile)
    //{
    //    string nameCate = product.Name;
    //    try { nameCate = categoryData.GetById(product.CategoryId).Name; }
    //    catch { }
    //    product.Image_thumb = Myfunction.CopyFileThumbnail(fileUpload_thumb.PostedFile, "Images/Product", nameCate + "-" + product.Name);
    //}
     //Copy image phu
    if (fileUploadImage_01.HasFile)
    {
        product.Image_01 = Myfunction.CopyFile(fileUploadImage_01.PostedFile, "Images/Product", product.Name + "-01");
    }
    if (fileUploadImage_02.HasFile)
    {
        product.Image_02 = Myfunction.CopyFile(fileUploadImage_02.PostedFile, "Images/Product", product.Name + "-02");
    }
    if (fileUploadImage_03.HasFile)
    {
        product.Image_03 = Myfunction.CopyFile(fileUploadImage_03.PostedFile, "Images/Product", product.Name + "-03");
    }
    if (fileUploadImage_04.HasFile)
    {
        product.Image_04 = Myfunction.CopyFile(fileUploadImage_04.PostedFile, "Images/Product", product.Name + "-04");
    }
    if (fileUploadImage_05.HasFile)
    {
        product.Image_05 = Myfunction.CopyFile(fileUploadImage_05.PostedFile, "Images/Product", product.Name + "-05");
    }  
    
    productData.Add(product);
    Myfunction.ThongBao(lbThongBao, "Thông tin sản phẩm đã được cập nhật !");
  }
  protected void btnOk_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (isValidate())
      {
        if (Request.QueryString["Id"] == null)
        {
          Add();
          ResetForm();
        }
        else
          Update();

      }
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  } 
}